<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Kesillo\Core;

class Router 
{
    private $_requestURI = NULL;
    private $_app = NULL;
    private $_controller = NULL;
    private $_method = NULL;
    private $_params = Array();
    private $_route = Array();
    
    public static function factory($url = NULL)
    {
        return new Router($url);
    }
    
    /*
     * Basic constructor. We jsut set $data
     */
    public function __construct($url = NULL)
    {
        $this->_requestURI = $url;
    }
    
    public function explodeUrl()
    {
        $this->_requestURI = explode('/', $this->_requestURI);
        
        //unset empty values in array
        for ($i = 0; $i <= count($this->_requestURI); $i++)
        {
            if ($this->_requestURI[$i] == NULL)
                unset($this->_requestURI[$i]);
        }
        
        $this->makeAppRouteMap($this->_requestURI);
    }
    
    /**
     * if $requestURI is length:
     * 1 = method
     * 2 = controller / method
     * 3 = app / controller / method
     * 
     * all params are delimited with ":"
     * 
     * the idea is to have something like app/controller/method/params but we'll just
     * implement the boave 2 ways.
     * 
     * @param type $url
     */
    private function makeAppRouteMap($url = NULL)
    {
        
        $rt = Array();
                
        if (count($url) == 1) {
            $this->_method = $this->_requestURI[1];
            
            $rt = array(
                'method' => $this->_method
            );
            
            $this->_route = $rt;
        }
        
        if (count($url) == 2) {
            $this->_controller = $this->_requestURI[1];
            $this->_method = $this->_requestURI[2];
            
            $rt = array(
                'controller' => $this->_controller,
                'method' => $this->_method
            );
            
            $this->_route = $rt;
        }
        
        if (count($url) == 3) {
            $this->_app = $this->_requestURI[1];
            $this->_controller = $this->_requestURI[2];
            $this->_method = $this->_requestURI[3];
            
            $rt = array(
                'app' => $this->_app,
                'controller' => $this->_controller,
                'method' => $this->_method
            );
            
            $this->_route = $rt;
        }
        
        if (count($url) > 3) {
            //everything after the 3 placeholder is considered a param
            // this doesnt work right. fix later
            for ($i = 4; $i <= count($url); $i++)
            {
                $this->_params = array(
                    $url[$i] => $url[$i+1]
                );
            }
            
            $this->_app = $this->_requestURI[1];
            $this->_controller = $this->_requestURI[2];
            $this->_method = $this->_requestURI[3];
            
            $rt = array(
                'app' => $this->_app,
                'controller' => $this->_controller,
                'method' => $this->_method,
                'params' => $this->_params
            );
            
            $this->_route = $rt;
            
        }
    }
    
    public function resolveRoute()
    {
        $this->explodeUrl();
        
        return $this->_route;
    }
}